import 'dart:convert';
import 'dart:io';
import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutterupdatedemo/update_dialog.dart';
import 'package:flutterupdatedemo/update_entity.dart';
import 'package:dio/dio.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:path_provider/path_provider.dart';
import 'package:package_info/package_info.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return MyHomeState();
  }
}

class MyHomeState extends State<MyHomePage> {
  UpdateEntity entity;

  startUpdate() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    var inputData = {
      "version": packageInfo.buildNumber,//本地版本號，上送服務器後，返回最新的一個版本，可傳可不傳
      "playform": Platform.isAndroid ? "android" : "ios"//設備平台
    };
    await Future.delayed(Duration(seconds: 2),(){});
//    Response res =
//        await Dio().get("https://www.baidu.com", queryParameters: inputData);
//    var resData = res.data;
    var outputData = {
      "code": 1,//服務器响应码
      "data": {
        "updateContent": "1....2......",
        "updateUrl": "http://",
        "isForceUpdate": false,
      }
    };
    String str = jsonEncode(outputData);

    UpdateEntity entity = UpdateEntity().fromJson(jsonDecode(str)["data"]);
    await Future.delayed(Duration(microseconds: 500));
    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return WillPopScope(
            onWillPop: () async => false, //重点此举
            child: Dialog(
              backgroundColor: Colors.transparent,
              insetPadding: EdgeInsets.symmetric(horizontal: 60.0, vertical: 0),
              child: UpdateDialog(entity),
            ));
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      body: Center(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            OutlineButton(
                child: Text('手動更新'),
                onPressed: () {
                  showUpdateDialog();
                }),
            OutlineButton(
                child: Text('模擬網絡請求更新'),
                onPressed: () {
                  startUpdate();
                })
          ],
        ),
      ),
    );
  }

  showUpdateDialog() async {
    entity = UpdateEntity();
    entity.updateContent = "1.修復bug";
    entity.isForceUpdate = false;
    entity.updateUrl =
        "https://fga1.market.xiaomi.com/download/AppStore/0ac6165a340b9472e3f7adf71b91c78343927d1e2/com.xiaomi.youpin.apk";
    await Future.delayed(Duration(microseconds: 500));
    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return Dialog(
          backgroundColor: Colors.transparent,
          insetPadding: EdgeInsets.symmetric(horizontal: 60.0, vertical: 0),
          child: UpdateDialog(entity),
        );
      },
    );
  }
}
